# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time
  #protect_from_forgery # See ActionController::RequestForgeryProtection for details

  # Scrub sensitive parameters from your log
  # filter_parameter_logging :password
  def exec_sql(sql)
    ActiveRecord::Base.connection.execute(sql)
  end

  def div_err(s)
    "<div class='err'>#{s}</div>"
  end

  def exec_cmd(cmd)
    system(cmd)
  end

  def sha1(s)
    Digest::SHA1.hexdigest(s)
  end

  def get_random_code
    Digest::SHA1.hexdigest("#{Time.now}#{random_string(8)}")
  end

  def random_string(len)
    #generat a random password consisting of strings and digits
    chars = ("a".."z").to_a + ("A".."Z").to_a + ("0".."9").to_a
    newpass = ""
    1.upto(len) { |i| newpass << chars[rand(chars.size-1)] }
    return newpass
  end

  def current_user
    return session[:user]
  end

  def get_ip
    request.env['HTTP_X_FORWARDED_FOR'] || request.env['REMOTE_ADDR']
  end

  def read_binary(path)
    File.open path, "rb" do |f| f.read end
  end

  def login_required
    if !session[:user]
      redirect_to :controller=>"user", :action=>"login"
    end
  end

  def admin_required
    if !session[:user]
      redirect_to :controller=>"user", :action=>"login"
    else
      if session[:user].admin_flag=='N'
        redirect_to :controller=>"welcome", :action=>"unauto"
      end
    end
  end

  def fix_price(n)
    return "%.2f" % n
  end

  def thai_baht(amount)
    number = amount.to_s
    txtnum1 = ['ศูนย์','หนึ่ง','สอง','สาม','สี่','ห้า','หก','เจ็ด','แปด','เก้า','สิบ']
    txtnum2 = ['','สิบ','ร้อย','พัน','หมื่น','แสน','ล้าน']
    number.gsub!(",","")
    number.gsub!(" ","")
    number.gsub!("บาท","")
    numbers = number.split(".")
    if(numbers.length>2)
      return "มีเครื่องหมาย '.' มากกว่า 1 ตัว"
    end
    strlen = numbers.first.length
    convert = ""
    0.upto(strlen-1) do |i|
      n = numbers.first[i].chr.to_i
      if (n!=0)
        if ( i == (strlen-1) and n == 1)
          convert = convert + "เอ็ด"
        elsif ( i == (strlen-2) and n == 2)
          convert = convert + "ยี่"
        elsif ( i == (strlen-2) and n == 1)
          convert = convert + ""
        else
        #	puts "n = #{n.chr.to_i}"
          convert = convert + txtnum1[n]
        end
        convert = convert + txtnum2[strlen-i-1]
      end
    end
    convert = convert + "บาท"
    if(numbers[1]=="0" or numbers[1]=="00" or numbers[1]=="" or numbers[1]==nil)
      convert = convert + "ถ้วน"
    else
      strlen = numbers[1].length
      if strlen==1
        numbers[1] = numbers[1]+"0"
        strlen = numbers[1].length
      end
      0.upto(strlen-1) do |i|
        n = numbers.last[i].chr.to_i
        if(n!=0)
          if(i==(strlen-1) and n==1)
            convert = convert + 'เอ็ด'
          elsif(i==(strlen-2) and n==2)
            convert = convert + 'ยี่'
          elsif(i==(strlen-2) and n==1)
            convert = convert + ''
          else
            convert = convert + txtnum1[n]
          end
          convert = convert + txtnum2[strlen-i-1]
        end
      end
      convert = convert + 'สตางค์'
    end
    return convert
  end

  def get_t_test_star(df, v)
    t_star = ''
    if df<=0
      t_star = ''
    else
      begin
        s = Student.find(:first, :conditions=>["df>=?", df], :order=>"df")
        if (v>=s.p01) || (v<=s.p01*-1)
          t_star = '***'
        elsif (v>=s.p05) || (v<=s.p05*-1)
          t_star = '**'
        elsif (v>=s.p10) || (v<=s.p10*-1)
          t_star = '*'
        else
          t_star = ''
        end
      rescue
        t_star = ''
      end
    end
    return "<font color='red'>#{t_star}</font>"
  end

end

class String
  def to_utf8
    cd = Iconv.new("UTF-8", "TIS-620")
    cd.iconv(self)
  end

  def to_tis
    cd = Iconv.new("TIS-620", "UTF-8")
    cd.iconv(self)
  end

  def to_tis620
    cd = Iconv.new("UTF-8", "TIS-620")
    cd.iconv(self)
  end
end
